const express = require('express');
const mongRouter = express.Router();
const goodsmongoose = require('../db/goodsDB');
const path= require('path');
const  logonBool = require('../middleware/logonBool');
const viewPath = path.resolve(__dirname,'../')
mongRouter.get('/logon',(req,res)=>{
    res.sendFile(viewPath+"/html/login.html");
});
mongRouter.get('/register',(req,res)=>{
    res.sendFile(viewPath+"/html/register.html");
});
mongRouter.get('/hPage',(req,res)=>{
    res.sendFile(viewPath+"/html/homepage.html");
});
mongRouter.get('/center',logonBool,(req,res)=>{
    res.sendFile(viewPath+'/html/center.html')
});
mongRouter.get('/getGoods',logonBool,(req,res)=>{
    goodsmongoose.find({username:req.session.username}).then((data)=>{
        res.send(data);
    })
    
});
mongRouter.get('/adddata',logonBool,(req,res)=>{
    res.sendFile(viewPath+'/html/adddata.html');
})
mongRouter.get('/',(req,res)=>{
    res.redirect('/hPage');
})

mongRouter.get('/exit',logonBool,(req,res)=>{
    req.session.destroy(()=>{
        res.redirect('/hPage');
    })
});
mongRouter.get('/goodsEdit',logonBool,(req,res)=>{
    res.sendFile(viewPath+'/html/goodsEdit.html');
});
mongRouter.get('/getAppoint',logonBool,(req,res)=>{
    let {id} = req.query;
    goodsmongoose.findOne({_id:id}).then((data)=>{
        res.send(data);
    })

})
mongRouter.get('/delete',logonBool,(req,res)=>{
    let {id} = req.query;
    console.log(id);
    goodsmongoose.deleteOne({_id:id}).then((data)=>{
        console.log(data);
        // res.redirect('/hPage');
        res.send(data);
    });
})
mongRouter.get('/getName',(req,res)=>{
    res.send(req.session.username);
});
module.exports = mongRouter;